/*
Author: Maciej Oczko
Date: Janueary 2012
*/
#include "GLTexture.h"

GLTexture::~GLTexture()
{
	if(!IsEmpty()) Unload();
}

bool GLTexture::Load(std::wstring p_FilePath)
{
	Resource::Load(p_FilePath);
	m_Identifier = p_FilePath;
	return (m_ref_count == 0) ? Load() : true;
}

bool GLTexture::Load()
{
#ifdef DEBUG
	TRACEW("Texture " << std::wstring(m_Identifier.begin() + m_Identifier.find_last_of('\\') + 1, m_Identifier.end()) << " loading...");
#endif
		m_pTexture = SOIL_load_OGL_texture(
			WStringToString(m_Identifier).c_str(), 
			SOIL_LOAD_AUTO,
			(m_pTexture == 0 ? SOIL_CREATE_NEW_ID : m_pTexture),
			(m_pTexture == 0 ? SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT : SOIL_FLAG_DDS_LOAD_DIRECT)
		);
	m_IsLoaded = (m_pTexture != 0);
	return m_IsLoaded;
}

void GLTexture::Unload()
{
	Texture::Unload();
	if(m_IsLoaded && m_ref_count == 0)
	{
		glDeleteTextures(1, &m_pTexture);
		m_IsLoaded = false;
#ifdef DEBUG
		TRACEW("Texture " << std::wstring(m_Identifier.begin() + m_Identifier.find_last_of('\\') + 1, m_Identifier.end()) << " unloaded");
#endif
	}
}

bool GLTexture::Reload()
{
	return m_IsLoaded ? true : Load();
}

size_t GLTexture::GetSize()
{
	size_t sizeOfData = 0;
	//REFGUID refguid = GUID();
	//m_pTexture->GetPrivateData(refguid, 0, &sizeOfData);
	return sizeOfData;
}
